import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import notification from 'ant-design-vue/es/notification'
import {
  VueAxios
} from './axios'
import {
  ACCESS_TOKEN,
  ACCESS_TOKEN_Refresh,
  STOPNOTION
} from '@/store/mutation-types'
import Qs from 'qs'
import {
  RefreshToken,
} from "@/api/login";


const request = async (props) => {
  console.log("22222", props)


  console.log("33")
  const data = await service({
    ...props
  });
  console.log("55", data)
  if (data == undefined) {
    notification.error({
      message: '错误',
      description: '登陆超时请重新登录'
    })
    store.dispatch('Logout').then(() => {
      setTimeout(() => {
        window.location.reload()
      }, 1000)
    })
  }

  const {
    code
  } = data

  if (code === 401) {
    notification.error({
      message: '错误',
      description: '登陆超时请重新登录'
    })
    store.dispatch('Logout').then(() => {
      setTimeout(() => {
        window.location.reload()
      }, 1000)
    })

  } else {
    // 正常情况-进入主界面
    return data
  }


}

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  timeout: 10000, // 请求超时时间
  headers: {
    'Source': 'gmweb',
    'Content-Type': 'application/x-www-form-urlencoded' // 请求头变成对应的json格式
  },
})

const err = (error) => {
  console.log('error', error)
  if (error.response) {
    const data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      notification.error({
        message: 'Forbidden',
        description: data.message
      })
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      notification.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}

// request interceptor请求拦截器
service.interceptors.request.use(config => {
  // 登入成功 缓存token
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    // 让每个请求携带自定义 token 请根据实际情况自行修改
    config.headers['Authorization'] = token
  }
  return config
}, err)

// response interceptor 响应拦截器
service.interceptors.response.use((response) => {
  console.log('response interceptor:' + response.data.code)
  if (response.data.code === 401) {
    notification.error({
      message: '错误',
      description: response.data.msg
    })
    store.dispatch('Logout').then(() => {
      setTimeout(() => {
        window.location.reload()
      }, 1000)
    })
  }
  return response.data
  // if (response.data.code === 200) {
  //   return response.data
  // } else if (response.data.code === 400) {
  //   notification.error({
  //     message: '错误',
  //     description: '参数有误'
  //   })
  //   // response.data
  // } else if (response.data.code === 500) {
  //   let msg = ''
  //   if(response.data.msg == "Cannot read property 'code' of undefined"){
  //     msg = '网络繁忙，请稍后再试'
  //   }else{
  //     msg = response.data.msg
  //   }
  //   if (response.data.msg == 'Bad credentials') {
  //     notification.error({
  //       message: '错误',
  //       description: '账户密码有误'
  //     })
  //   } else {
  //     if(Vue.ls.get(STOPNOTION)){
  //       Vue.ls.set(STOPNOTION,false)
  //       setTimeout(() => {
  //         Vue.ls.set(STOPNOTION,true)
  //       }, 3000);
  //       notification.error({
  //         message: '错误',
  //         description: msg
  //       })
  //     }
  //   }
  //   //response.data
  // } else if (response.data.code === 200001) {
  //   notification.error({
  //     message: '错误',
  //     description: response.data.msg
  //   })
  //   //response.data
  //   return
  // } else if (response.data.code === 300001) {
  //   notification.error({
  //     message: '错误',
  //     description: response.data.msg
  //   })
  // } else if (response.data.code === 401) {
  //   notification.error({
  //     message: '错误',
  //     description: response.data.msg
  //   })
  //   store.dispatch('Logout').then(() => {
  //     setTimeout(() => {
  //       window.location.reload()
  //     }, 1000)
  //   })
  // }

}, function (err) {
  if (err.code == 'ECONNABORTED') {
    notification.error({
      message: '错误',
      description: '网络连接超时,请稍后再试！'
    })
  } else {

  }
})

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  // service as axios,
  request as axios
}